// pages/classify/classify.js

let network = require("../../utils/network.js")
let app = getApp();
let url = app.globalData.url;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    height:'',
    goodsTypeId:0, //列表id,
    goodsList:'',
    goodsTypeList: '',
    discount: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({
      discount: wx.getStorageSync('discount')
    })
    wx.getSystemInfo({
      success(res) {
        that.setData({
          height: res.windowHeight + 'px'
        })
      }
    })
    that.goodsList()
    
  },

  goodsList(){
    var that = this
    wx.request({
      url: url+'/AppletsApi/Index/goodsTypeList.html',
      data: {
        goodsTypeId:that.data.goodsTypeId
      },
      method:'post',
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        that.setData({
          goodsList: res.data.data.goodsList,
          goodsTypeList: res.data.data.goodsTypeList,
        })

      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 分类
  type:function(e){
    var that = this
    let id = e.currentTarget.dataset.id
    that.setData({
      goodsTypeId:id
    })
    
    that.goodsList()
  },
  // 全部
  all:function(){
    var that = this
    that.setData({
      goodsTypeId: 0
    })
    that.goodsList()
  },
  // 跳转详情页
  detail:function(e){
    var that = this
    let path = e.currentTarget.dataset.path
    console.log(path)
    wx.navigateTo({
      url: path,
    })
  },
  //
  cart:function(e){
    var that = this
    var id = e.currentTarget.dataset.id
    network.request({
      url: url + '/AppletsApi/Goods/addToCart.html',
      data: {
        applet_openid: wx.getStorageSync('applet_openid'),
        goods_id: id,
        goods_num: 1,
      },
      method: 'post',
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        console.log(res)
        wx.reLaunch({
          url: '/pages/cart/cart'
        })
      }
    })


  }


})